<template>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="父级菜单">
            <el-select v-model="form.pId" placeholder="请选择" clearable>
                <el-option label="/" :value="0" />
                <el-option v-for="item in menuPrantName" :label="item.menuName" :value="item.menuId" />
            </el-select>
        </el-form-item>
        <el-form-item label="菜单名称">
            <el-input v-model="form.menuName" />
        </el-form-item>
        <el-form-item label="跳转的Url地址">
            <el-input v-model="form.url" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
const route = useRoute();
const router = useRouter();
const form = reactive({
    menuName: '',
    url: '',
    pId: '',
    menuOrder: 1,
    createName: localStorage.getItem("userName"),
    createTime: new Date(),
    isDeleted: false
})

//分页
const page = reactive({
    menuName:'',
    pageIndex: 1,
    pageSize: 10000,
    totalCount: 0,
    totalPage: 0
})

const menuPrantName = ref([]);
const getMenuPrantName = () => {
    axios.get("/api/Menu/Show", {
        params: {
            MenuName: page.menuName,
            PageIndex: page.pageIndex,
            PageSize: page.pageSize
        }
    })
        .then(res => {
            menuPrantName.value = res.data.data.list
            page.totalCount = res.data.data.totalCount
            page.totalPage = res.data.data.totalPage
            // console.log(menuPrantName.value)
        })
        .catch(err => {
            console.log(err);
        })
}

const onSubmit = () => {
    // console.log(form)
    axios.post("/api/Menu/Create", form)
        .then(res => {
            if (res.data.code == 0) {
                ElMessage.success("添加成功");
                router.push({
                    name:'MenuList'
                })
            }
        })
        .catch(err => {
            console.log(err);
        })
}

onMounted(() => {
    getMenuPrantName();
})
</script>